package cn.hughie.imooc;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @Author yuh
 * @Date Created in 上午11:15 2018/5/26
 * @Description
 */
public class AtomicReference {

    private static class Demo {
        volatile Integer id;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }
    }

    private static AtomicReferenceFieldUpdater<Demo, Integer> atomicReferenceFieldUpdater = AtomicReferenceFieldUpdater.newUpdater(Demo.class, Integer.class, "id");


    public static void main(String[] args) {
        Demo demo = new Demo();
        demo.setId(1);
        atomicReferenceFieldUpdater.compareAndSet(demo,1,2);
        atomicReferenceFieldUpdater.compareAndSet(demo,3,5);
        System.out.println(demo.id);
    }


}
